import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
const app = getApp()
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        info: {},
        apiField: {
            module: 'monthPrice',
            api: 'Detail',
            data: 'info',
            search: {
                id: null
            }
        },
        vehicleImgs: [],
        vehiclePic: null,
        memberId: ''
    },
    onLoad(options) {
        this.data.apiField.search.id = options[0]
        if (options[1]) {
            this.data.memberId = options[1]
        }
        this.fetchData()
    },
    async fetchData() {
        this.handleFetchData({}, 'apiField', true)
    },
    async selectVehicleImg(e) {
        var file = e.detail
        let imgBase64 = wx.getFileSystemManager().readFileSync(file.tempFilePaths[0], "base64")

        //压缩
        const res = await wx.$Api.face.Compress({
            data: {
                ImageBase64String: imgBase64
            },
            hasLoading: true
        })

        this.setData({
            vehiclePic: 'data:image/png;base64,' + res.Data,
            vehicleImgs: [{ url: file.tempFilePaths[0] }]
        })
    },
    deleteVehicleImg(e) {
        var index = e.detail.index
        const vehicleImgs = this.data.vehicleImgs
        vehicleImgs.splice(index, 1)
        this.setData({
            vehiclePic: null,
            vehicleImgs
        })
    },
    async submitForm(e) {
        const formData = e.detail.value
        if (this.data.info.MonthType == 1 || this.data.info.MonthType == 3) {
            if (this.data.info.OpenDoorType == 1 && !formData.MasterCard) {
                wx.$Fun.showToast("请输入门禁卡号")
                return
            }
        }
        if (!this.data.vehiclePic) {
            wx.$Fun.showToast("请上传车辆照片")
            return
        }

        try {
            var req = {
                MonthPriceId: this.data.apiField.search.id,
                PowerCard: formData.PowerCard,
                MasterCard: formData.MasterCard,
                FirstCard: formData.FirstCard,
                PlateNo: formData.PlateNo,
                PicBase64String: this.data.vehiclePic,
            }

            if (this.data.memberId) {
                req.MemberId = this.data.memberId
            }
            const res = await wx.$Api.vehicle.Add({
                data: req
            })
            this.updateNeedRefresh(true)

            wx.$Fun.toPage({
                page: 'vehiclePay',
                params: {
                    pageType: 'redirectTo',
                    "0": res.Data,
                    "1": this.data.memberId ? 1 : 2,
                }
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
}))